// pages/dishes/dishes.js
const tool = require('../../tool/Holidays.js');
const getdata = require("../../tool/GetDate.js")
const util = require("../../utils/util.js")
var app = getApp();
var districtID = 0;
var hallID = 0;
var date;
var deadLine = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dishesInfo: [
    //   {
    //   dishesClassName: "素菜",
    //   dishesList: [{
    //     name: "炒豆芽",
    //     dishesClassName: "素菜",
    //     attributeName: "周一",
    //     isSelected: true,
    //     isDisable: true,
    //     imgUrl: "/images/meal.png",
    //   },
    //   {
    //     name: "苦瓜炒蛋",
    //     dishesClassName: "素菜",
    //     isSelected: true,
    //     isDisable: true,
    //     imgUrl: "/images/meal.png",
    //   },
    //   {
    //     name: "炒白菜",
    //     dishesClassName: "素菜",
    //     attributeName: "周一",
    //     isSelected: false,
    //     isDisable: true,
    //     imgUrl: "/images/meal.png",
    //   },
    //   {
    //     name: "拌豆腐",
    //     dishesClassName: "素菜",
    //     isSelected: true,
    //     isDisable: true,
    //     imgUrl: "/images/meal.png",
    //   },
    //   {
    //     name: "炒土豆",
    //     dishesClassName: "素菜",
    //     isSelected: false,
    //     isDisable: true,
    //     imgUrl: "/images/meal.png",
    //   },
    //   ]
    // },
    // {
    //   dishesClassName: "荤菜",
    //   dishesList: [
    //     {
    //       name: "红烧肉",
    //       dishesClassName: "荤菜",
    //       attributeName: "周一",
    //       isSelected: false,
    //       isDisable: true,
    //       imgUrl: "/images/meal.png",
    //     },
    //     {
    //       name: "宫保鸡丁",
    //       dishesClassName: "荤菜",
    //       attributeName: "周一",
    //       isSelected: false,
    //       isDisable: true,
    //       imgUrl: "/images/meal.png",
    //     },
    //     {
    //       name: "红烧鲫鱼",
    //       dishesClassName: "荤菜",
    //       isSelected: false,
    //       isDisable: true,
    //       imgUrl: "/images/meal.png",
    //     },
    //     {
    //       name: "清蒸鱼",
    //       dishesClassName: "荤菜",
    //       attributeName: "周一",
    //       isSelected: true,
    //       isDisable: true,
    //       imgUrl: "/images/meal.png",
    //     },
    //   ]
    // },
    ],
    diningHallArray: [],
    objectDiningHallArray: [],
    index: 0,
    districtArray: [],
    objectDistrictArray: [],
    districtIndex: 0,
    date: '',
    startDate: '',
    endDate: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    date = util.formatDateDiff2(1);
    this.setData({
      startDate: util.formatDateDiff2(1),
      endDate: util.formatDateDiff2(6),
      date: util.formatDateDiff2(1),
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  getDistrict: function () {
    let that = this;
    getdata.getdata({
      url: "gbt/BasicInfo/DistrictInfo/",
      data: app.globalData.myUserData.districtID,
      success: function (res) {
        console.log(res.root);
        if (res.root) {
          let districtIndex = wx.getStorageSync("districtIndex");
          if (!districtIndex) {
            districtIndex = 0;
          }

          districtID = res.root.districtArray[districtIndex].id;
          that.getDiningHall(districtID);

          that.setData({
            districtArray: res.root.districtTitleArray,
            objectDistrictArray: res.root.districtArray,
            districtIndex: districtIndex,
          });
        }
      },
    });
  },

  getDiningHall: function (districtID) {
    let that = this;
    //根据获取到的区域信息加载食堂
    getdata.getdata({
      url: "gbt/BasicInfo/DiningHall/",
      data: districtID,
      success: function (res) {
        console.log(res.root);
        if (res.root) {
          let diningHallIndex = wx.getStorageSync("diningHallPickerIndex");
          if (!diningHallIndex) {
            diningHallIndex = 0;
          }
          //暂时默认
          hallID = res.root.diningHallArray[diningHallIndex].id;
          deadLine = res.root.diningHallArray[diningHallIndex].deadline;
          that.getDishes(districtID, hallID);

          that.setData({
            diningHallArray: res.root.diningHallTitleArray,
            objectDiningHallArray: res.root.diningHallArray,
            index: diningHallIndex,
          });
        }
      },
    });
  },

  getDishes: function (districtID, hallID) {
    let that = this;
    let param = {};
    param["districtID"] = districtID;
    param["hallID"] = hallID;
    param["date"] = date;
    param["userID"] = app.globalData.myUserData.userID;
    getdata.postdata({
      url: "gbt/BasicInfo/Dishes",
      data: JSON.stringify(param),
      success: function (res) {
        console.log(res.root);
        if (res.root) {
          that.setData({
            dishesInfo: res.root.data,
          });
        }
        else {
          that.setData({
            dishesInfo: [],
          });
        }
      },
    });
  },

  optDishes: function (param) {
    let that = this;
    getdata.postdata({
      url: "gbt/Dishes/OptDishes",
      data: JSON.stringify(param),
      success: function (res) {
        console.log(res.root);
        if (res.root) {
          wx.showToast({
            title: `${res.root.message}`,
            icon: "none",
            duration: 3000,
          });

          that.getDishes(districtID,hallID);
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDistrict();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {

  // },

  bindDistrictPickerChange: function (e) {
    this.setData({
      districtIndex: e.detail.value
    })
    let district = this.data.objectDistrictArray[e.detail.value];
    districtID = district.id;
    this.getDiningHall(districtID);
    wx.setStorageSync("districtIndex", e.detail.value);
  },

  bindDiningHallPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let hall = this.data.objectDiningHallArray[e.detail.value];
    hallID = hall.id;
    deadline = hall.deadline;
    this.getDishes(districtID, hallID);

    this.setData({
      index: e.detail.value
    });
    wx.setStorageSync("diningHallPickerIndex", e.detail.value);
  },

  onDateChange: function (e) {
    console.log(e);
    date = e.detail.value;
    this.getDishes(districtID, hallID);
    this.setData({
      date: e.detail.value,
    });
  },

  onCore: function (e) {
    console.log(e);
    let param = {};
    param["dishesClassID"] = e.detail.dishesClassID;
    param["dishesID"] = e.detail.id;
    param["districtID"] = districtID;
    param["userID"] = app.globalData.myUserData.userID;
    param["date"] = date;
    param["hallID"] = hallID;
    param["deadLine"] = deadLine;
    this.optDishes(param);
  },

  onDetail:function(e){
    wx.navigateTo({
      url: '/pages/dishes_detail/dishes_detail?state=search&hallID='+hallID+'&id='+e.detail.id,
    })
  }
})